<?php

namespace app\admins\controller;

use think\Controller;
use think\Db;
use Util\SysDb;

class Examine extends Controller
{
    public function del()
    {
        $id = input('param.examineId');
        $this->db = new SysDb();
        $del = $this->db->table('examinelist')->where(['examineid' => $id])->delete();
        if ($del) {
            $resetId = $this->db->table('examinelist')->lists();
            $re = 1;
            foreach ($resetId as $rid) {
                $this->db->table('examinelist')->where(['id' => $rid['id']])->update(['examineid' => $re, 'id' => $re]);
                ++$re;
            }
            $re = 1;
            exit(json_encode(['code' => 0, 'msg' => '删除成功']));
        } else {
            exit(json_encode(['code' => 1, 'msg' => '删除失败']));
        }
    }

    public function allDel()
    {
        $allId = input('param.allId');

        $this->db = new SysDb();
        foreach ($allId as $id) {
            $del = $this->db->table('examinelist')->where(['examineid' => $id])->delete();
        }
        if ($del) {
            $resetId = $this->db->table('examinelist')->lists();
            $re = 1;
            foreach ($resetId as $rid) {
                $this->db->table('examinelist')->where(['id' => $rid['id']])->update(['examineid' => $re, 'id' => $re]);
                ++$re;
            }
            $re = 1;
            exit(json_encode(['code' => 0, 'msg' => '删除成功']));
        } else {
            exit(json_encode(['code' => 1, 'msg' => '删除失败']));
        }
    }

    public function doagree()
    {
        $data['releaseStatus'] = '2';
        $id = trim(input('param.newsId'));
        $class = trim(input('param.class'));
        $this->db = new SysDb();
        $this->db->table('examinelist')->where(['userNumber' => $id, 'userHobby' => $class])->update($data);
        exit(json_encode(['code' => 0, 'msg' => '审核成功']));
    }

    public function dodisagree()
    {
        $data['releaseStatus'] = '1';
        $id = trim(input('param.newsId'));
        $class = trim(input('param.class'));
        $this->db = new SysDb();
        $this->db->table('examinelist')->where(['userNumber' => $id, 'userHobby' => $class])->update($data);
        exit(json_encode(['code' => 0, 'msg' => '审核成功']));
    }

    public function studentList()
    {
        $user = input('param.user');
        $page = input('param.page');
        $limit = input('param.limit');
        $val = trim(input('param.key'));

        $this->db = new SysDb();
        //模糊查询
        if ($val == '') {
            //索引
            $index = [];
            //数据
            $data1 = $this->db->table('examinelist')->where(['userNumber' => $user])->lists();

            $center = [];
            foreach ($data1 as $d) {
                array_push($center, $d);
                $index['data'] = $center;
            }
            //分页
            $count = $this->db->table('examinelist')->where(['userNumber' => $user])->count();
            $tol = ($page - 1) * $limit;

            $list = db('examinelist')->where(['userNumber' => $user])->limit($tol, $limit)->select();
            $index['code'] = 0;
            $index['count'] = $count;
            $index['data'] = $list;
        } else {
            //索引
            $index = [];
            //数据
            //$data1 = $this->db->table('releaselist')->where(['Did' => 't1_1'], ['releaseName', 'like', '%'.$val.'%'])->lists();
            $data1 = DB::name('examinelist')->where(['userNumber' => $user])->where('userHobby', 'like', '%'.$val.'%')->select();
            //var_dump($data1);

            $center = [];
            foreach ($data1 as $d) {
                array_push($center, $d);
                $index['data'] = $center;
            }
            if (!$data1) {
                $index['msg'] = '暂无此记录';
                $index['code'] = 2;
            }
            //分页
            $count = $this->db->table('examinelist')->where(['userNumber' => $user])->count();
            $tol = ($page - 1) * $limit;

            $list = DB::name('examinelist')->where(['userNumber' => $user])->where('userHobby', 'like', '%'.$val.'%')->limit($tol, $limit)->select();

            $index['count'] = $count;
            $index['data'] = $list;
            $index['code'] = 0;
        }

        $index_json = json_encode($index);

        exit($index_json);
    }
}
